const User = require('../model/user.model')
const UserDetail = require('../model/user_detail.model')
const { Op, Sequelize } = require("sequelize");
const seq = require('../db/seq');
class UserService {
    // 因为存入数据库 是异步的
    async createUser(username, password, email, phone) {
        const user_id = Math.floor(Date.now() / 1000);
        // 对象
        // 当我们属性名和传过来的值一致的时候可以简写
        // await表达式: 返回成功promise对象的值
        const res = await User.create({ username, password, user_id })
        const res1 = await UserDetail.create({ user_id, email, phone })
        //    返回到controller
        if (res1 && res1) {
            return {
                code: "10000",
                message: "注册成功",
                result: "",
                username: username
            }
        }

    }
    async getUserInfo({ id, username, password, is_admin }) {

        const whereOpt = {}
        // 短路运算
        id && Object.assign(whereOpt, { id })  // 不为空则添加到whereOpt中
        username && Object.assign(whereOpt, { username })
        password && Object.assign(whereOpt, { password })
        is_admin && Object.assign(whereOpt, { is_admin })

        const res = await User.findOne({
            // 查询的内容
            attributes: ['id', 'username', 'password', 'is_admin', 'user_id'],
            where: whereOpt
        })
        return res ? res.dataValues : ''
    }
    async getCurrentUser(param) {
        const { user_id, username } = param;
        let result
        try {
            await seq.query(`( SELECT Users.*,UserDetails.phone,UserDetails.email,UserDetails.signature,UserDetails.tag,UserDetails.sex,UserDetails.job FROM Users LEFT JOIN UserDetails ON Users.user_id = UserDetails.user_id WHERE Users.user_id=${user_id} )`, { type: seq.QueryTypes.SELECT }).then((res) => {
                result = res
            })

        } catch (err) {
            console.log(err, '🐷')
        }

        console.log(result)
        return result[0] ? result[0] : 'error'
    }
    async updateAvatarService(param) {
        const { user_img, user_id } = param
        const res = await User.update({ user_img: user_img }, {
            where: {
                user_id: user_id,
            }
        })
        return res[0] > 0 ? 'success' : 'error'
    }
    async updateUserDetailService(param) {
        console.log(param)
        const { phone, user_id, job, tag, email, signature, sex } = param
        const res = await UserDetail.update({ phone: phone, job: job, tag: tag, email: email, signature: signature, sex: sex }, {
            where: {
                user_id: user_id,
            }
        })
        console.log(res)
        return res[0] > 0 ? 'success' : 'error'
    }
    async jobSubjectService() {
        let result;
        //这篇文章的获赞量
        await seq.query(`SELECT COUNT(user_id) AS count,job FROM UserDetails GROUP BY job`, { type: seq.QueryTypes.SELECT }).then((res) => {
            //  return 不能写到promise中
            console.log(res)
            result = res
        })
        return result
    }
    async userCountService() {
        let result
        try {
            //   seq是定义的数据库实例  必须加await
            await seq.query(`(SELECT COUNT(DISTINCT(user_id)) AS count FROM Users)`, { type: seq.QueryTypes.SELECT }).then((res) => {
                result = res
                console.log(res)
            })
            return result

        } catch (err) {
            console.log(err)
            return err
        }
    }
}
// 导出后 在控制器controller中使用
module.exports = new UserService()